package com.auerdi.ui.fragment.e_my.orders.childs.detail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auerdi.R;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.AddressLists;
import com.example.egghttp.retrofit.beans.goods.Address;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by tse on 16/2/14.
 */
public class ADOrderDetailFragMent extends SwipeBackFragment {

    @BindView(R.id.statusBar)
    View statusBar;
    @BindView(R.id.title_left)
    RelativeLayout titleLeft;
    @BindView(R.id.title)
    TextView title;
    List<Address> mAddressList = new ArrayList<>();

    public static ADOrderDetailFragMent newInstance() {

        Bundle args = new Bundle();
        ADOrderDetailFragMent fragment = new ADOrderDetailFragMent();
        fragment.setArguments(args);
        return fragment;
    }

    Unbinder unbinder;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.order_detail_fragment, container, false);

        unbinder = ButterKnife.bind(this, view);
        titleLeft.setOnClickListener(v -> pop());
        View tpview = attachToSwipeBack(view);


        return tpview;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);


        RetrofitClient.getPDAClient().create(AdService.class).
                addressLists()
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BOb<AddressLists>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        L.d("hcia", "e:" + e);
                    }

                    @Override
                    public void onNext(BOb<AddressLists> addressListsBOb) {

                        List<Address> addressList = addressListsBOb.getBody().getAddress_list();

//                        if (addressList.size() == 0) {
//                            EventBus.getDefault().post(ADADDresssAddFragment.newInstance(""));
//                        }

                        mAddressList.clear();
                        mAddressList.addAll(addressList);

//                        L.d("hcia", " addressList.size():" + addressList.size());
                    }
                });
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


}
